<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class isp_vendor_invoice extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('isp_vendor_invoice_model', 'IspVendorInvoice');
		//$this->load->model('isp_combo_model', 'Combo');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Invoice Vendor";	
		//$data['arr_vendor']		= $this->Combo->ISPVendor();	

		$query = $this->db->query("SELECT kode_invoice FROM (SELECT * FROM isp_vendor_invoice ORDER BY kode_invoice DESC) WHERE rownum <= 1");
		$getquery = $query->row_array();

		if($getquery)
			$MaxID = $getquery['kode_invoice'];
		else
			$MaxID = "IV0000000";

		$NextID = (int) substr($MaxID,2,7);

		$NextID++;
		
		$data['kode_invoice_auto'] = "IV" . sprintf("%07s",$NextID);

		viewByTemplate('isp_vendor_invoice/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->IspVendorInvoice->GetJSON();
	}

	function getdata2()
	{
		$this->secure_session();
		echo $this->IspVendorInvoice->GetJSON2();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation		
		if (post("kode_invoice") == '') {
			$error->Add("kode_invoice", "Kode Invoice", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM isp_vendor_invoice WHERE kode_invoice = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("kode_invoice"))))."'")) > 0) {
			$error->Add("kode_invoice", "Kode Invoice", $tpl_duplicate);
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$tgl_old9 = str_replace(" ", "", $this->input->post('tgl_invoice'));
				$tahun_new9 = substr($tgl_old9, 6, 4);
			    $bulan_new9 = substr($tgl_old9, 3, 2);
			    $hari_new9  = substr($tgl_old9, 0, 2);
			    $tgl_new9 = $tahun_new9."-".$bulan_new9."-".$hari_new9;

				$data	=  array(
					'kode_invoice' => $this->input->post('kode_invoice'),
					'kode_kontrak' => $this->input->post('kode_kontrak'),
					//'kode_vendor' => $this->input->post('kode_vendor'),
					'nama_vendor' => $this->input->post('nama_vendor'),
					'harga' => $this->input->post('harga'),
					'tgl_invoice' => $tgl_new9,
					'status' => $this->input->post('status'),
					'total_bayar' => $this->input->post('total_bayar'),
					'keterangan' => $this->input->post('keterangan'),
					);

				if (post("mode") == "create")
				{
					//$data['create_date']	= NOW;
					//$data['create_user']	= USERID;

					$this->IspVendorInvoice->Add($data);

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.role_id ?? ""));

				}
				else if (post("mode") == "edit")
				{
					//$data['modify_date']	= NOW;
					//$data['modify_user']	= USERID;
					
					$this->IspVendorInvoice->Update($data, 
										array('kode_invoice'	=> $data['kode_invoice']));

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS002", "edit/" + (data.role_id ?? ""));

				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result		= "";
		$kode_invoice 	= get('kode_invoice');
		
		if ($kode_invoice != "")
		{
			// Delete Single
			$arr_rule	= array('kode_invoice' => $kode_invoice);
			$ls_result = $this->IspVendorInvoice->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('kode_invoice' => $row['kode_invoice']);
				$ls_result .= $this->IspVendorInvoice->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}
}
?>